package com.joe.demo.nio.file;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * @Description 拷贝多级目录
 * @Author gaojw
 * @Date 2024-08-28
 */
public class TestFilesCopy {

    public static void main(String[] args) throws IOException {
        String source = "G:\\note\\test\\1";
        String target = "G:\\note\\test\\2";
        Files.walk(Paths.get(source)).forEach(path -> {
            try {
                String targetPath = path.toString().replace(source, target);
                // 是目录，创建
                if (Files.isDirectory(path)) {
                    Files.createDirectory(Paths.get(targetPath));

                }
                // 是文件， 拷贝
                else if (Files.isRegularFile(path)){
                    Files.copy(path, Paths.get(targetPath));
                }
            } catch (IOException exception){
                exception.printStackTrace();
            }

        });
    }
}
